﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Text;

namespace ITProject.WebUI.Infrastructure
{
    public class MailProvider
    {
        /// <summary>
        /// metoda wysylajaca emaile
        /// </summary>
        /// <param name="messageText">tekst wiadomosci</param>
        /// <param name="destinationAddress">adres docelowy</param>
        /// <param name="subject">temat wiadomosci</param>
        /// <param name="?"></param>
        private static void SendMessage(string messageText, string destinationAddress, string subject)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("itproject2012@gmail.com");
            message.To.Add(new MailAddress(destinationAddress));
            message.Subject = subject;
            message.Body = messageText;

            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("testITProject2012@gmail.com", "testmail");
            client.EnableSsl = true;
            client.Port = 587;
            client.Send(message);
        }

        public static void SendActivationLink(int UserId, string mail, string hash)
        {

            string message = "Kliknij w ponizszczy link, aby sie zarejestrowac \n " +
                HttpContext.Current.Request.Url.ToString().Replace("Register","") +
                MVC.Account.ActionNames.Activate + "?hash=" + hash;
            SendMessage(message, mail, "Aktywacja");
        }

        #region "Random string generator"

        private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden
        public static string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

        #endregion
    }
}